package com.redwine.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.redwine.app.APP;
import com.redwine.app.R;
import com.redwine.app.net.GsonRequest;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.BaseResponse;
import com.redwine.app.net.responses.MyOfferResponse;
import com.redwine.app.utils.BitmapUtils;
import com.redwine.app.utils.Global;
import com.redwine.app.utils.Utils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的报价
 * 作者：gassion on 17/9/9 18:35
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class MyOfferAdapter extends BaseAdapter {

    private Context context;

    private List<MyOfferResponse.DataEntityX.DataEntity> datalist;


    public MyOfferAdapter(Context context) {
        this.context = context;
    }


    /**
     * 下拉加载数据
     *
     * @param datalist
     */
    public void setData(int page, List<MyOfferResponse.DataEntityX.DataEntity> datalist) {
        if (page > 1) {
            if (null != this.datalist && !this.datalist.isEmpty())
                this.datalist.addAll(datalist);

        } else {
            if (null != this.datalist && !this.datalist.isEmpty())
                this.datalist.clear();
            this.datalist = datalist;
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (null != datalist) {
            return datalist.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (null != datalist) {
            return datalist.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.activity_my_offer_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final MyOfferResponse.DataEntityX.DataEntity dataEntity = datalist.get(position);
        MyOfferResponse.DataEntityX.DataEntity.PublishInfoEntity publish_info = dataEntity.getPublish_info();
        BitmapUtils.loadImage(viewHolder.mMyOfferIvShoppingPic, dataEntity.getPic(), BitmapUtils.THE_ARTICLE);
        viewHolder.mMyOfferTvShoppingCount.setText(publish_info.getPrice());
        viewHolder.mMyOfferTvTime.setText(dataEntity.getDate());
        viewHolder.mMyOfferTvShoppingName.setText(publish_info.getTitle());
        viewHolder.mMyOfferTvName.setText(publish_info.getNickname());
        viewHolder.mMyOfferTvShoppingTitle.setText("");
        //1 报价中 2 采纳
        if ("1".equals(dataEntity.getStatusX())) {
            viewHolder.mMyOfferTvShoppingDelete.setVisibility(View.VISIBLE);
            viewHolder.mMyOfferTvAdoptStatus.setText("报价中");
        } else {
            viewHolder.mMyOfferTvShoppingDelete.setVisibility(View.GONE);
            viewHolder.mMyOfferTvAdoptStatus.setText("采纳");
        }
        Utils.setProcurementStatus(viewHolder.mMyOfferTvShoppingStatus, Integer.valueOf(publish_info.getStatusX()));
        viewHolder.mMyOfferTvShoppingDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestDeletePriceData(dataEntity.getId(), position);
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        public View rootView;
        public TextView mMyOfferTvShoppingStatus;
        public TextView mMyOfferTvShoppingTitle;
        public TextView mMyOfferTvShoppingDelete;
        public ImageView mMyOfferIvShoppingPic;
        public TextView mMyOfferTvShoppingName;
        public TextView mMyOfferTvShoppingCount;
        public TextView mMyOfferTvName;
        public TextView mMyOfferTvTime;
        public TextView mMyOfferTvAdoptStatus;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.mMyOfferTvShoppingStatus = (TextView) rootView.findViewById(R.id.my_offer_tv_shopping_status);
            this.mMyOfferTvShoppingTitle = (TextView) rootView.findViewById(R.id.my_offer_tv_shopping_title);
            this.mMyOfferTvShoppingDelete = (TextView) rootView.findViewById(R.id.my_offer_tv_shopping_delete);
            this.mMyOfferIvShoppingPic = (ImageView) rootView.findViewById(R.id.my_offer_iv_shopping_pic);
            this.mMyOfferTvShoppingName = (TextView) rootView.findViewById(R.id.my_offer_tv_shopping_name);
            this.mMyOfferTvShoppingCount = (TextView) rootView.findViewById(R.id.my_offer_tv_shopping_count);
            this.mMyOfferTvName = (TextView) rootView.findViewById(R.id.my_offer_tv_name);
            this.mMyOfferTvTime = (TextView) rootView.findViewById(R.id.my_offer_tv_time);
            this.mMyOfferTvAdoptStatus = (TextView) rootView.findViewById(R.id.my_offer_tv_adopt_status);
        }

    }

    /**
     * 删除报价接口
     */
    private void requestDeletePriceData(String priceId, final int position) {
        String url = NetworkConstants.PRICE_DELETE_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", Global.user.getUid());
        params.put("price_id", priceId);
        GsonRequest<BaseResponse> request = new GsonRequest<>(url, Request.Method.POST,
                BaseResponse.class, false, params, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse response) {
                Utils.showToast(response.getMsg());
                if (response.isSuccess()) {
                    datalist.remove(position);
                    notifyDataSetChanged();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Utils.showToast(error.getMessage());
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }
}
